<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Cache;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }


    //获取网站基础信息
    public function getBase(){

        $key = 'index-config';
        $data = Cache::get($key);
        if(empty($data)){
            $data = Db::name('config')
                ->where('group','basic')
                ->select();
            $ll = [];
            foreach ($data as $k=>$v){
                if(in_array($v['id'],[1,2,19,20,21,22,23,24,25,26,27,28])){
                    $ll[$v['name']] = $v['value'];
                }
            }
            $data = $ll;
            Cache::set($key,json_encode($data),30*3600);
        }else{
            $data = json_decode($data,true);
        }

        $this->success('请求成功',$data);
    }
    //关于们
    public function gywm(){

        $key = 'index-gywm';
        $data = Cache::get($key);
        if(empty($data)){
            $data = Db::name('gywm')
                ->where('id',1)
                ->find();
            Cache::set($key,json_encode($data),30*3600);
        }else{
            $data = json_decode($data,true);
        }
        $this->success('请求成功',$data);
    }
    //联系我们
    public function lxwm(){
        $key = 'index-lxwm';
        $data = Cache::get($key);
        if(empty($data)){
            $data = Db::name('gywm')
                ->where('id',2)
                ->find();
            Cache::set($key,json_encode($data),30*3600);
        }else{
            $data = json_decode($data,true);
        }
        $this->success('请求成功',$data);
    }
    //轮播图
    public function getBanner(){
        $key = 'index-getBanner';
        $data = Cache::get($key);
        if(empty($data)){
            $data = Db::name('banner')
                ->order(['sort'=>'asc','id'=>'asc'])
                ->select();
            Cache::set($key,json_encode($data),30*3600);
        }else{
            $data = json_decode($data,true);
        }
        $this->success('请求成功',$data);
    }
    //栏目
    public function getCate(){
        $key = 'index-getCate';
        $data = Cache::get($key);
        if(empty($data)){
            $data = Db::name('cate')
                ->order(['sort'=>'asc','id'=>'asc'])
                ->select();
            Cache::set($key,json_encode($data),30*3600);
        }else{
            $data = json_decode($data,true);
        }
        $this->success('请求成功',$data);
    }
    //首页文章列表
    public function getIndexArticle(){
        $key = 'index-getArticle_';
        $data = Cache::get($key);
        if(empty($data)){
            $data = Db::name('cate')
                ->order(['sort'=>'asc','id'=>'asc'])
                ->select();

            foreach ($data as $k=>$v){
                $list = Db::name('article')
                    ->where('cate_id',$v['id'])
                    ->field('id,title')
                    ->order(['is_top_switch'=>'desc','sort'=>'asc','id'=>'desc'])
                    ->page(1,8)
                    ->select();

                $data[$k]['articleList'] = $list;
            }

            Cache::set($key,json_encode($data),30*3600);
        }else{
            $data = json_decode($data,true);
        }
        $this->success('请求成功',$data);
    }
    //根据分类获取文章列表
    public function getArticle(){
        $catId = input('cateId','');
        $page = input('page',1);
        $size = input('size',8);
        if(empty($catId)){
            $this->error('参数错误');
        }
        $key = 'index-getArticle_'.$catId.'_'.$page.'_'.$size;
        $data = Cache::get($key);
        if(empty($data)){
            $list = Db::name('article')
                ->where('cate_id',$catId)
                ->order(['is_top_switch'=>'desc','sort'=>'asc','id'=>'desc'])
                ->page($page,$size)
                ->select();
            $total =  Db::name('article')
                ->where('cate_id',$catId)
                ->count();
            foreach ($list as $k=>$v){
                $list[$k]['fb_time'] = date('Y-m-d H:i:s',$v['createtime']);
            }
            $data = [
                'list'=>$list,
                'total'=>$total
            ];
            Cache::set($key,json_encode($data),30*3600);
        }else{
            $data = json_decode($data,true);
        }
        $this->success('请求成功',$data);
    }
    //文章详情
    public function getArticleDetail(){
        $id = input('id','');
        if(empty($id)){
            $this->error('参数错误');
        }
        $key = 'index-getArticleDetail_'.$id;
        $data = Cache::get($key);
        if(empty($data)){
            $data = Db::name('article')
                ->where('id',$id)
                ->find();
            $data['fb_time'] = date('Y-m-d H:i:s',$data['createtime']);

            $last = Db::name('article')
                ->where('id','<',$id)
                ->field('id,title')
                ->order(['is_top_switch'=>'desc','sort'=>'asc','id'=>'desc'])
                ->find();
            $next = Db::name('article')
                ->where('id','>',$id)
                ->field('id,title')
                ->order(['is_top_switch'=>'desc','sort'=>'asc','id'=>'desc'])
                ->find();
            $data = [
                'detail'=>$data,
                'last'=>$last,
                'next'=>$next,
            ];
            Cache::set($key,json_encode($data),30*3600);
        }else{
            $data = json_decode($data,true);
        }
        $this->success('请求成功',$data);
    }
    //友情链接
    public function getYqlj(){
        $key = 'index-getYqlj';
        $data = Cache::get($key);
        if(empty($data)){
            $data = Db::name('yqlj')
                ->order(['sort'=>'asc','id'=>'asc'])
                ->select();
            Cache::set($key,json_encode($data),30*3600);
        }else{
            $data = json_decode($data,true);
        }
        $this->success('请求成功',$data);
    }

}
